import { _decorator,color,Component ,instantiate,Label,Node, Prefab, Sprite, SpriteFrame } from 'cc';
import { Core } from '../Core/Core';
import { EColor, EEvent, EMartialSystem, EPet, EState, EView } from '../Config/Enum';
import { ResourceManager } from '../Core/ResourceManager';
import { PicPath, PrefabPath } from '../Config/ResourceConfig';
import { PetManager } from '../PetManager';
import { PetData } from '../PetData';
import { PetConfig } from '../Config/PetConfig';



const { ccclass, property } = _decorator;

@ccclass('PetView')
export class PetView extends Component {
    ndPet: Node = null;
    ndPetList: Node = null;
    ndBoard: Node = null;
    lbIsSelected: Label = null;





    lbLevel: Label
    lbDesc: Label
    lbName: Label
    petPrefab: any;
    lbStrength: Label
    lbAgility: Label
    lbIntelligence: Label
    lbSkillDesc: Label
    lbWuxing: any;
    pet:EPet
    petData: PetData
    config



    protected onLoad(): void {
        this.ndBoard = this.node.getChildByName("Board")
        this.ndPet = this.ndBoard.getChildByName("Pet")

        this.ndPetList = this.ndBoard.getChildByName("PetList")
        this.lbLevel = this.ndBoard.getChildByName("Level").getComponent(Label)
        this.lbDesc = this.ndBoard.getChildByName("Desc").getComponent(Label)
        this.lbName = this.ndBoard.getChildByName("Name").getComponent(Label)
        this.lbWuxing = this.ndBoard.getChildByName("Wuxing").getComponent(Label)
        this.lbIsSelected = this.ndBoard.getChildByName("IsSelected").getComponent(Label)

        this.lbStrength = this.ndBoard.getChildByName("Attribute").getChildByName("Strength").getComponent(Label)
        this.lbAgility = this.ndBoard.getChildByName("Attribute").getChildByName("Agility").getComponent(Label)
        this.lbIntelligence = this.ndBoard.getChildByName("Attribute").getChildByName("Intelligence").getComponent(Label)
        this.lbSkillDesc = this.ndBoard.getChildByName("Skill").getChildByName("Desc").getComponent(Label)




        
    }

    protected onEnable(): void {
        Core.Instance.event.on(EEvent.PetUpdate, this.onPetUpdate, this);

        this.petData = Core.Instance.pets.get(Core.Instance.currentPet)
        this.config = PetConfig[Core.Instance.currentPet]

        // Core.Instance.currentView = EView.MartialView;

        // console.log("MartialView打开，当前已安排工作的义士：", Core.Instance.assignedPlayers);

        // // 当前已安排工作的义士的数据

        this.loadData();

        
    }

    protected onDisable(): void {
        Core.Instance.event.off(EEvent.PetUpdate, this.onPetUpdate, this);

        //Core.Instance.event.off(EEvent.FarmingUpdate, this.onFarmingUpdate, this);
        
    }

    protected start(): void {
        
    }

    onPetUpdate(){
        this.loadData();
    }

    loadData(){

        this.ndPet.removeAllChildren()
        //this.playerPrefab = Core.Instance.prefabMap.get(player);
        let petPrefab = ResourceManager.Instance.getRes(PrefabPath[Core.Instance.currentPet])
        //console.log("宠物petPrefab", petPrefab);


        let node = instantiate(petPrefab as Prefab);
        node.setScale(3, 3)
        this.ndPet.addChild(node)
        node.setPosition(0,0,0)
        node.getComponent(PetManager).initState(Core.Instance.currentPet)
        node.getComponent(PetManager).state = EState.IdleFront

        this.checkIsSelected();
        this.lbLevel.string = "等级：" + this.petData.level.toString();
        this.lbDesc.string = this.petData.desc;
        this.lbName.string = this.petData.name;
        //console.log("宠物this.petData.wuxing",this.config.wuxing);
        //console.log("宠物EColor[this.config.wuxing]",EColor[this.config.wuxing]);
        this.lbWuxing.color = color().fromHEX(EColor[this.config.wuxing]);
        this.lbWuxing.string = this.petData.wuxing.toString();
        


        this.lbStrength.string = "全队力量：" + (this.petData.strength*100).toString() + "%";
        this.lbAgility.string = "全队敏捷：" + (this.petData.agility*100).toString() + "%";
        this.lbIntelligence.string = "全队智力：" + (this.petData.intelligence*100).toString() + "%";

        this.lbSkillDesc.string = this.petData.skillDesc;






    }
    checkIsSelected() {
        this.lbIsSelected.string = this.petData.assigned ? "上阵中" : "未上阵";
        this.lbIsSelected.color = this.petData.assigned ? color().fromHEX("#ff0000") : color().fromHEX("#00ff00");
    }







    onButtonPetGoBattle(){

        // 如果当前灵兽已经上阵，则下阵，否则上阵
        if (this.petData.assigned) {
            this.petData.assigned = false;
            Core.Instance.showTips("灵兽下阵成功")
        }else{        
            this.petData.assigned = true;
            Core.Instance.showTips("灵兽上阵成功")
        }
        Core.Instance.players.forEach((playerData, player) => {
            playerData.calculateAttributes(true);
        })
        this.checkIsSelected();
        Core.Instance.save()
    }




}


